import tkinter as tk
from tkinter import filedialog,dialog
import os

root = tk.Tk()
root.title('nihao')
root.geometry('500x500')

file_path = ''
file_text = ''

text_1 = tk.Text(root,width=50,height=10,bg='orange',font=('Arial',12))
text_1.pack()

def open_file():
    global file_path
    global file_text
    file_path = filedialog.askopenfilename(title=u'选择文件',initialdir=(os
    .path.expanduser('')))
    print('打开文件:',file_path)
    if file_path is not None:
        with open(file=file_path,mode='r+',encoding='utf-8')as file:
            file_text = file.read()
        text_1.insert('end',file_text)

def save_file():
    global file_path
    global file_text
    file_path = filedialog.asksaveasfile(title=u'选择文件')
    print('保存文件',file_path)
    file_text = text_1.get('1.0',tk.END)
    if file_path is not None:
        with open(file=file_path,mode='a+',encoding='utf-8') as f:
            f.write(file_text)
        text_1.delete('1.0',tk.END)
        dialog.Dialog(None,{'title':'filr modified','text':'保存成功',
    'bitmap': 'warning', 'default': 0,'strings': ('OK', 'Cancle')})
        print('保存成功')

bt1 = tk.Button(root, text='打开文件', width=15, height=2, command=open_file)
bt1.pack()
bt2 = tk.Button(root, text='保存文件', width=15, height=2, command=save_file)
bt2.pack()

root.mainloop()